//===============================================================================
//
// LookupTableCollection
//
// PURPOSE: 
// 
//
// NOTES: 
// 
//
//===============================================================================
//
// Copyright (C) 2003 Wallis Software Solutions
// All rights reserved.
//
// THIS CODE AND INFORMATION IS PROVIDED 'AS IS' WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR
// FITNESS FOR A PARTICULAR PURPOSE.
//
//===============================================================================

using System;
using System.Data;
using Agile.Common;
using Agile.Genie.Descriptors;
using GeneratorCustomization;

namespace Descriptors.GeneratorCustomization
{
    /// <summary>
    /// LookupTableCollection
    /// </summary>
    [Obsolete]
    public class LookupTableCollection : AgileCollection
    {
        #region Preserved Region - Developer Hand Written Code

        private static LookupTableCollection _allLookupTables;

        /// <summary>
        /// Gets ALL of the columns listed in the excluded columns table.
        /// </summary>
        public static LookupTableCollection All
        {
            get
            {
                if (_allLookupTables == null)
                    LoadLookupTables();
                return _allLookupTables;
            }
        }

        /// <summary>
        /// Load the excluded columns details from the xml file.
        /// </summary>
        private static void LoadLookupTables()
        {
            _allLookupTables = Build();
            DataRowCollection rows = GeneratorsData.GetDataRowsFor(LookupTable.TableName);
            foreach (DataRow row in rows)
            {
                LookupTable lookupTable = LookupTable.Build(row["Database"].ToString()
                                                         , row["TableName"].ToString()
                                                         , row["DisplayColumn"].ToString());
                _allLookupTables.Add(lookupTable);
            }
        }

        /// <summary>
        /// Get all filtered columns for a specific table name.
        /// </summary>
        /// <param name="tableName">Name of the table.</param>
        /// <returns></returns>
        public LookupTableCollection FilteredBy(string tableName)
        {
            LookupTableCollection filtered = Build();
            foreach (LookupTable lookupTable in this)
            {
                if (lookupTable.Table.ToLower().Equals(tableName.ToLower()))
                    filtered.Add(lookupTable);
            }
            return filtered;
        }

        /// <summary>
        /// Get all filtered columns for a specific table in a specific database.
        /// </summary>
        /// <param name="database">DatabaseName name.</param>
        /// <param name="tableName">Name of the table.</param>
        /// <returns></returns>
        public LookupTableCollection FilteredBy(string database, string tableName)
        {
            LookupTableCollection filtered = Build();
            foreach (LookupTable lookupTable in this)
            {
                if (lookupTable.Table.ToLower().Equals(tableName.ToLower())
                    && lookupTable.Table.ToLower().Equals(database.ToLower()))
                    filtered.Add(lookupTable);
            }
            return filtered;
        }

        /// <summary>
        /// Checks if the given Lookup Table is in the collection.
        /// </summary>
        /// <param name="tableToLookFor">Table to look for in the collection.</param>
        /// <returns>True if the item is in the collection.</returns>
        private bool Contains(LookupTable tableToLookFor)
        {
            foreach (LookupTable lookupTable in this)
            {
                if (tableToLookFor.DisplayColumn.ToLower().Equals(lookupTable.DisplayColumn.ToLower())
                    && tableToLookFor.Table.ToLower().Equals(lookupTable.Table.ToLower())
                    && tableToLookFor.Database.ToLower().Equals(lookupTable.Database.ToLower()))
                    return true;
            }
            return false;
        }

        /// <summary>
        /// Checks if the collection contains a table with the given name (in the given database).
        /// NOT case sensitive!
        /// </summary>
        /// <param name="tableName">The name of the table to look for in the collection.</param>
        /// <param name="database">database the table is in.</param>
        /// <returns>True if the item is in the collection.</returns>
        private bool ContainsTable(string database, string tableName)
        {
            return FindTable(database, tableName) != null;
        }

        /// <summary>
        /// Returns the LookupTable with matching database and table name.
        /// Must be an EXACT match
        /// </summary>
        /// <param name="database">Name of the database</param>
        /// <param name="tableName">table name</param>
        /// <returns></returns>
        public LookupTable FindTable(string database, string tableName)
        {
            foreach (LookupTable lookupTable in this)
            {
                if ((lookupTable.Table.Equals(tableName, System.StringComparison.InvariantCultureIgnoreCase))
                    && DatabaseMatches(lookupTable, database))
                    return lookupTable;
            }
            return null;
        }

        /// <summary>
        /// Returns the LookupTable for a database table.
        /// </summary>
        /// <param name="table">a database table</param>
        public LookupTable FindTable(DatabaseTable table)
        {
            return FindTable(table.DatabaseName, table.Name);
        }

        /// <summary>
        /// Check if the database is a match.
        /// </summary>
        /// <param name="table">table to match the database name against.</param>
        /// <param name="database">database name to check.</param>
        /// <returns></returns>
        private bool DatabaseMatches(LookupTable table, string database)
        {
            // if the database has not been set then the rule applies to all tables in all db's
            if (string.IsNullOrEmpty(table.Database))
                return true;

            return table.Database.ToLower().Equals(database.ToLower());
        }

        #endregion // Preserved Region - Developer Hand Written Code

        #region Constructors and Factories

        /// <summary>
        /// Constructor
        /// </summary>
        internal LookupTableCollection()
        {
        }

        /// <summary>
        /// Instantiate a new LookupTableCollection
        /// </summary>
        public static LookupTableCollection Build()
        {
            return new LookupTableCollection();
        }

        #endregion

        #region Strongly Typed IList Implementations

        /// <summary>
        /// Returns the LookupTable that is at the given index in the collection.
        /// </summary>
        public LookupTable this[int index]
        {
            get { return InnerList[index] as LookupTable; }
        }

        /// <summary>
        /// Add a new LookupTable to the collection
        /// </summary>
        /// <param name="lookupTable">lookupTable to add to the collection.</param>
        public void Add(LookupTable lookupTable)
        {
            if (Contains(lookupTable))
            {
                string message = string.Format("Tried adding a LookupTable to the collection but it is already in it.");
                throw new AgileCommonException(message);
            }
            List.Add(lookupTable);
        }

        /// <summary>
        /// Remove a LookupTable from the collection
        /// </summary>
        /// <param name="lookupTable">lookupTable to remove from the collection.</param>
        public void Remove(LookupTable lookupTable)
        {
            InnerList.Remove(lookupTable);
        }

        /// <summary>
        /// Searches for the specified LookupTable and returns the zero-based index
        /// of the first occurrence within the section of the
        /// collection that starts at the
        /// specified index and contains the specified number of elements.
        /// </summary>
        /// <param name="lookupTable">lookupTable to get the index of from the collection.</param>
        /// <returns>The index of the item in the collection.</returns>
        public int IndexOf(LookupTable lookupTable)
        {
            return InnerList.IndexOf(lookupTable);
        }

        /// <summary>
        /// Searches for the specified LookupTable and returns the zero-based index
        /// of the first occurrence within the section of the
        /// collection that starts at the
        /// specified index and contains the specified number of elements.
        /// </summary>
        /// <param name="lookupTable">lookupTable to get the index of from the collection.</param>
        /// <param name="startIndex">The zero-based starting index of the search.</param>
        /// <returns>The index of the item in the collection.</returns>
        public int IndexOf(LookupTable lookupTable, int startIndex)
        {
            return InnerList.IndexOf(lookupTable, startIndex);
        }

        /// <summary>
        /// Searches for the specified LookupTable and returns the zero-based index
        /// of the first occurrence within the section of the
        /// collection that starts at the
        /// specified index and contains the specified number of elements.
        /// </summary>
        /// <param name="lookupTable">lookupTable to get the index of from the collection.</param>
        /// <param name="startIndex">The zero-based starting index of the search.</param>
        /// <param name="count">The number of elements in the section to search.</param>
        /// <returns>The index of the item in the collection.</returns>
        public int IndexOf(LookupTable lookupTable, int startIndex, int count)
        {
            return InnerList.IndexOf(lookupTable, startIndex, count);
        }

        /// <summary>
        /// Inserts the LookupTable into the collection at the specified index.
        /// </summary>
        /// <param name="index">The zero-based index at which value should be inserted.</param>
        /// <param name="lookupTable">lookupTable to insert into the collection.</param>
        public void Insert(int index, LookupTable lookupTable)
        {
            InnerList.Insert(index, lookupTable);
        }

        /// <summary>
        /// Copies a range of elements from the collection of LookupTable's to a compatible
        /// one-dimensional System.Array, starting at the specified index of the target array.
        /// </summary>
        /// <param name="lookupTables">The one-dimensional System.Array that is the destination of the elements copied from the collection. The System.Array must have zero-based indexing.</param>
        /// <param name="index">The zero-based index at which copying begins.</param>
        public void CopyTo(LookupTable[] lookupTables, int index)
        {
            List.CopyTo(lookupTables, index);
        }

        #endregion
    }
}